True if any place in target holds a value equal to value, false otherwise.
array_contains(target, value) -> bool| Name | Type |
|---|---|
target | array |
value | element |
bool
true if any place in target, an array, holds a value equal to value, false otherwise.
A plain left-to-right search - target does not need to be sorted, and nothing about calling this changes target itself. Useful for "is this one of the allowed ones" checks - a key number that opens a door, a tile number that counts as water.
If you also want to know where the value is, array_find gives its place, or -1 if it is not there.
Numbers are equal when they are the same number, and text is equal when every letter is the same. It is the same search list_contains makes, for the container that has a fixed number of places.
An array has exactly the places array_length says, and the search looks at them from the front until it finds a match. A longer array takes longer, and a search that finds nothing looks at every place.
A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_contains inside a script searches the caller's array, whatever its size.
| You do this | What happens |
|---|---|
| search for a value that is not there | false |
| search for a value that is there more than once | true |
| give it a list | refused when checked. A list has its own, list_contains |
var doors = array_create(3, 0);
doors[0] = 4;
doors[1] = 8;
doors[2] = 15;
var has8 = array_contains(doors, 8); // true
var has99 = array_contains(doors, 99); // falseA plain left-to-right search (find, the GBA module's own alias for std::find) - O(n) in how many places target has, same cost as writing the loop by hand. Fine for the small, bounded arrays this hardware ever holds; nothing about target needs to be sorted. It is exactly list_contains, for the container that has a fixed number of places.